{-# LANGUAGE OverloadedStrings #-}

-- spinning dodecahedron animation -- made during process but not used in final result

import Data.List
import Text.LaTeX
import Text.LaTeX.Base.Class
import Text.LaTeX.Base.Syntax
import Text.LaTeX.Packages.Graphicx
import Text.LaTeX.Packages.Geometry
import Util
import Vec

main :: IO ()
main = printdoc doc

doc :: Monad m => LaTeXT_ m
doc = do
    mapM_ id $ intersperse (raw "\n\\mbox{}\\clearpage{}\n") $ fmap renderd [0.0, pi / 32 .. pi * 63 / 32]

renderd :: Monad m => Double -> LaTeXT_ m
renderd x = do
    mapM_ rendere $ polymap (vadd (0.0, 0.0, 3.0)) $ polyrotate (dodecahedron 0.5) (0.0, x, 0.3)

rendere :: LaTeXC l => (Vec3, Vec3) -> l
rendere (a, b) =
    if d > 0.001 then textblock' (vw $ fst c) (vw $ snd c) $ rotatebox' (-t * 180 / pi) $ fontsize (vw $ d * 0.280) (vw 0) "spinning"
    else ""
  where
    c = vadd a' $ vdiv (vsub b' a') 2
    d = vdist a' b'
    t = atan2 (snd $ vsub b' a') (fst $ vsub b' a')
    a' = vadd (0.5, sqrt 2 / 2) $ vproject a
    b' = vadd (0.5, sqrt 2 / 2) $ vproject b

polymap :: (Vec3 -> Vec3) -> [(Vec3, Vec3)] -> [(Vec3, Vec3)]
polymap f = fmap (\(a, b) -> (f a, f b))

polyrotate :: [(Vec3, Vec3)] -> Vec3 -> [(Vec3, Vec3)]
polyrotate vs t = polymap (flip v3rotate t) vs

dodecahedron :: Double -> [(Vec3, Vec3)]
dodecahedron r = connect dist $ vdodecahedron r
  where
    connect dist xs = [(x, y) | (x:ys) <- tails xs, y <- ys, vdist x y < dist]
    dist = r * (sqrt 3) * 2.01 / phi

vdodecahedron :: Double -> [Vec3]
vdodecahedron r = fmap (flip vmul $ r * sqrt 3) $ c vs vs'
  where
    c = (\vs vs' -> vs' ++ vs ++ (fmap (vcycle . vcycle) vs) ++ (fmap vcycle vs))
    vs = [(x, y, z) | x <- [phi, -phi], y <- [0], z <- [1 / phi, -1 / phi]]
    vs' = [(x, y, z) | x <- [-1, 1], y <- [-1, 1], z <- [-1, 1]]

phi :: Double
phi = (1 + sqrt 5) / 2
